package com.zl.learning.io.test.com.ronsoft.books.nio.regex;

/**
 * Created by zhaolei on 2017/4/26.
 */

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Validates email addresses.
 *
 * Regular expression found in the Regular Expression Library
 * at regxlib.com. Quoting from the site,
 * "Email validator that adheres directly to the specification
 * for email address naming. It allows for everything from
 * ipaddress and country-code domains, to very rare characters
 * in the username."
 *
 * @author Michael Daudel (mgd@ronsoft.com) (original)
 * @author Ron Hitchens (ron@ronsoft.com) (hacked)
 */
public class EmailAddressFinder
{
    public static void main (String[] argv)
    {
        if (argv.length < 1) {
            System.out.println ("usage: emailaddress ...");
        }
// 编译电子邮件地址检测器的模式
        Pattern pattern = Pattern.compile (
                "([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]"
                        + "{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))"
                        + "([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)",
                Pattern.MULTILINE);
// 为模式制作一个Matcher对象
        Matcher matcher = pattern.matcher ("");
// 循环遍历参数并在每个寻找地址
        for (int i = 0; i < argv.length; i++) {
            boolean matched = false;
            System.out.println ("");
            System.out.println ("Looking at " + argv [i] + " ...");
// 将匹配器复位，查看当前实参字符串
            matcher.reset (argv [i]);
// 遇到匹配时循环
            while (matcher.find( )){
// 找到一个匹配
                System.out.println ("\t" + matcher.group( ));
            matched = true;
        }
        if ( ! matched) {
            System.out.println ("\tNo email addresses found");
        }
    }
}
}
